home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / generic / mod_1.c < prev    next >
C/C++ Source or Header  |  1994-05-16  |  6KB  |  199 lines

  1. /* __mpn_mod_1(dividend_ptr, dividend_size, divisor_limb) --
  2.    Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
  3.    Return the single-limb remainder.
  4.    There are no constraints on the value of the divisor.
  5.  
  6.    QUOT_PTR and DIVIDEND_PTR might point to the same limb.
  7.  
  8. Copyright (C) 1991, 1993, 1994, Free Software Foundation, Inc.
  9.  
  10. This file is part of the GNU MP Library.
  11.  
  12. The GNU MP Library is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU Library General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or (at your
  15. option) any later version.
  16.  
  17. The GNU MP Library is distributed in the hope that it will be useful, but
  18. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  19. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  20. License for more details.
  21.  
  22. You should have received a copy of the GNU Library General Public License
  23. along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
  24. the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  25.  
  26. #include "gmp.h"
  27. #include "gmp-impl.h"
  28. #include "longlong.h"
  29.  
  30. #ifndef UMUL_TIME
  31. #define UMUL_TIME 1
  32. #endif
  33.  
  34. #ifndef UDIV_TIME
  35. #define UDIV_TIME UMUL_TIME
  36. #endif
  37.  
  38. /* FIXME: We should be using invert_limb (or invert_normalized_limb)
  39.    here (not udiv_qrnnd).  */
  40.  
  41. mp_limb
  42. #if __STDC__
  43. __mpn_mod_1 (mp_srcptr dividend_ptr, mp_size_t dividend_size,
  44.          mp_limb divisor_limb)
  45. #else
  46. __mpn_mod_1 (dividend_ptr, dividend_size, divisor_limb)
  47.      mp_srcptr dividend_ptr;
  48.      mp_size_t dividend_size;
  49.      mp_limb divisor_limb;
  50. #endif
  51. {
  52.   mp_size_t i;
  53.   mp_limb n1, n0, r;
  54.   int dummy;
  55.  
  56.   /* Botch: Should this be handled at all?  Rely on callers?  */
  57.   if (dividend_size == 0)
  58.     return 0;
  59.  
  60.   /* If multiplication is much faster than division, and the
  61.      dividend is large, pre-invert the divisor, and use
  62.      only multiplications in the inner loop.  */
  63.  
  64.   /* This test should be read:
  65.        Does it ever help to use udiv_qrnnd_preinv?
  66.      && Does what we save compensate for the inversion overhead?  */
  67.   if (UDIV_TIME > (2 * UMUL_TIME + 6)
  68.       && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME)
  69.     {
  70.       int normalization_steps;
  71.  
  72.       count_leading_zeros (normalization_steps, divisor_limb);
  73.       if (normalization_steps != 0)
  74.     {
  75.       mp_limb divisor_limb_inverted;
  76.  
  77.       divisor_limb <<= normalization_steps;
  78.  
  79.       /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB.  The
  80.          result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
  81.          most significant bit (with weight 2**N) implicit.  */
  82.  
  83.       /* Special case for DIVISOR_LIMB == 100...000.  */
  84.       if (divisor_limb << 1 == 0)
  85.         divisor_limb_inverted = ~(mp_limb) 0;
  86.       else
  87.         udiv_qrnnd (divisor_limb_inverted, dummy,
  88.             -divisor_limb, 0, divisor_limb);
  89.  
  90.       n1 = dividend_ptr[dividend_size - 1];
  91.       r = n1 >> (BITS_PER_MP_LIMB - normalization_steps);
  92.  
  93.       /* Possible optimization:
  94.          if (r == 0
  95.          && divisor_limb > ((n1 << normalization_steps)
  96.                  | (dividend_ptr[dividend_size - 2] >> ...)))
  97.          ...one division less... */
  98.  
  99.       for (i = dividend_size - 2; i >= 0; i--)
  100.         {
  101.           n0 = dividend_ptr[i];
  102.           udiv_qrnnd_preinv (dummy, r, r,
  103.                  ((n1 << normalization_steps)
  104.                   | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))),
  105.                  divisor_limb, divisor_limb_inverted);
  106.           n1 = n0;
  107.         }
  108.       udiv_qrnnd_preinv (dummy, r, r,
  109.                  n1 << normalization_steps,
  110.                  divisor_limb, divisor_limb_inverted);
  111.       return r >> normalization_steps;
  112.     }
  113.       else
  114.     {
  115.       mp_limb divisor_limb_inverted;
  116.  
  117.       /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB.  The
  118.          result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
  119.          most significant bit (with weight 2**N) implicit.  */
  120.  
  121.       /* Special case for DIVISOR_LIMB == 100...000.  */
  122.       if (divisor_limb << 1 == 0)
  123.         divisor_limb_inverted = ~(mp_limb) 0;
  124.       else
  125.         udiv_qrnnd (divisor_limb_inverted, dummy,
  126.             -divisor_limb, 0, divisor_limb);
  127.  
  128.       i = dividend_size - 1;
  129.       r = dividend_ptr[i];
  130.  
  131.       if (r >= divisor_limb)
  132.         r = 0;
  133.       else
  134.         i--;
  135.  
  136.       for (; i >= 0; i--)
  137.         {
  138.           n0 = dividend_ptr[i];
  139.           udiv_qrnnd_preinv (dummy, r, r,
  140.                  n0, divisor_limb, divisor_limb_inverted);
  141.         }
  142.       return r;
  143.     }
  144.     }
  145.   else
  146.     {
  147.       if (UDIV_NEEDS_NORMALIZATION)
  148.     {
  149.       int normalization_steps;
  150.  
  151.       count_leading_zeros (normalization_steps, divisor_limb);
  152.       if (normalization_steps != 0)
  153.         {
  154.           divisor_limb <<= normalization_steps;
  155.  
  156.           n1 = dividend_ptr[dividend_size - 1];
  157.           r = n1 >> (BITS_PER_MP_LIMB - normalization_steps);
  158.  
  159.           /* Possible optimization:
  160.          if (r == 0
  161.          && divisor_limb > ((n1 << normalization_steps)
  162.                  | (dividend_ptr[dividend_size - 2] >> ...)))
  163.          ...one division less... */
  164.  
  165.           for (i = dividend_size - 2; i >= 0; i--)
  166.         {
  167.           n0 = dividend_ptr[i];
  168.           udiv_qrnnd (dummy, r, r,
  169.                   ((n1 << normalization_steps)
  170.                    | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))),
  171.                   divisor_limb);
  172.           n1 = n0;
  173.         }
  174.           udiv_qrnnd (dummy, r, r,
  175.               n1 << normalization_steps,
  176.               divisor_limb);
  177.           return r >> normalization_steps;
  178.         }
  179.     }
  180.       /* No normalization needed, either because udiv_qrnnd doesn't require
  181.      it, or because DIVISOR_LIMB is already normalized.  */
  182.  
  183.       i = dividend_size - 1;
  184.       r = dividend_ptr[i];
  185.  
  186.       if (r >= divisor_limb)
  187.     r = 0;
  188.       else
  189.     i--;
  190.  
  191.       for (; i >= 0; i--)
  192.     {
  193.       n0 = dividend_ptr[i];
  194.       udiv_qrnnd (dummy, r, r, n0, divisor_limb);
  195.     }
  196.       return r;
  197.     }
  198. }
  199.